﻿using PlantsZombies.Common.GDI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace PlantsZombies.View
{
    public partial class FrmStart : Form
    {
        private Rectangle clickableArea;
        public FrmStart()
        {
            InitializeComponent();
            SetStyle(
           ControlStyles.ResizeRedraw
           | ControlStyles.DoubleBuffer
           | ControlStyles.UserPaint
           | ControlStyles.AllPaintingInWmPaint
           | ControlStyles.SupportsTransparentBackColor,
           true
           );
            // 设置窗体属性
            this.Resize += FrmStart_Resize;
            this.MouseDown += FrmStart_MouseDown;

            this.SuspendLayout();
            // 初始化可点击区域
            UpdateClickableArea();
            this.ResumeLayout(false);


            //string rootPath = Path.Combine(Application.StartupPath, "test"); // 替换为你的文件夹路径
            //string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; // 图片扩展名数组

            //// 使用LINQ查询所有图片文件
            //var imageFiles = Directory.EnumerateFiles(rootPath, "*.*", SearchOption.AllDirectories)
            //                           .Where(file => imageExtensions.Contains(Path.GetExtension(file).ToLower())).ToArray();

            //imageGDIs=new ImageGDI[imageFiles.Length];
            //string path = @"C:\Users\JS114295\Desktop\61cce029be6edd7c3a173a2ac20a6dd.bmp";
            //for (int i = 0; i < imageFiles.Length; i++)
            //{
            //    imageGDIs[i] = new ImageGDI(path);
            //}

        }

        private void FrmStart_MouseDown(object sender, MouseEventArgs e)
        {
            if (!clickableArea.Contains(e.Location)) return;

            FrmMain frmMain = null;
            using (FrmWaiting fw=new FrmWaiting())
            {
               ((Action)(() =>
                {
                    frmMain = new FrmMain();
                })).BeginInvoke(new AsyncCallback((ar) => (ar.AsyncState as FrmWaiting).DialogResult = DialogResult.OK), fw);
                fw.StartPosition = FormStartPosition.CenterScreen;
                fw.ShowDialog();
            }
            this.Hide();
            frmMain.ShowDialog();
            frmMain.Dispose();
            this.Show();
            frmMain.Dispose();
        }

        private void FrmStart_Resize(object sender, EventArgs e)
        {
            UpdateClickableArea();
        }
        private void UpdateClickableArea()
        {
            float zoomX = (float)this.Width / 973;
            float zoomY = (float)this.Height / 569;
            int areaWidth = (int)(180 * zoomX);
            int areaHeight = (int)(80 * zoomY);
            int areaX = (int)(200 * zoomX);
            int areaY = (int)(250 * zoomY);

            clickableArea = new Rectangle(areaX, areaY, areaWidth, areaHeight);
        }

        private void FrmStart_FormClosing(object sender, FormClosingEventArgs e)
        {
           
            this.Dispose();
        }
    }
}
